package com.woniu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.RedPacketPool;
import com.woniu.vo.RedPacketPageVO;
import com.woniu.vo.UserRedPacketMaxVO;
import com.woniu.vo.UserRedPacketVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
public interface RedPacketPoolMapper extends BaseMapper<RedPacketPool> {

    @Select("SELECT * FROM red_packet_pool AS rpp WHERE rpp.create_time BETWEEN #{startTime} AND #{endTime} AND rpp.already_received = 0 ORDER BY RAND() LIMIT 1")
    RedPacketPool findOneByNowDate(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("SELECT * FROM red_packet_pool AS rpp WHERE rpp.create_time BETWEEN #{startTime} AND #{endTime} AND rpp.already_received = 0 AND rpp.red_packet_money > #{minMoney} ORDER BY RAND() LIMIT 1")
    RedPacketPool findOneMaxByNowDate(@Param("startTime") String startTime, @Param("endTime") String endTime,@Param("minMoney") Double minMoney);

    @Select("SELECT IF(COUNT(*) > 0,true,false) FROM red_packet_pool AS rpp WHERE rpp.create_time BETWEEN #{startTime} AND #{endTime} AND rpp.already_received = 0")
    boolean existByNowDate(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("SELECT IF(COUNT(*) > 0,true,false) FROM red_packet_pool AS rpp WHERE rpp.create_time BETWEEN #{startTime} AND #{endTime} AND rpp.already_received = 1 AND rpp.receive_nuser_id = #{userId}")
    boolean existReceivedByNowDateAndUserId(@Param("startTime") String startTime, @Param("endTime") String endTime,@Param("userId") String userId);

    @Select("SELECT IF(COUNT(*) > 0,true,false) FROM red_packet_pool AS rpp LEFT JOIN n_user AS nu ON nu.id = rpp.receive_nuser_id WHERE rpp.create_time BETWEEN #{startTime} AND #{endTime} AND rpp.already_received = 1 AND nu.u_openid = #{uOpenid}")
    boolean existReceivedByNowDateAndUopenid(@Param("startTime") String startTime, @Param("endTime") String endTime,@Param("uOpenid") String uOpenid);


    @Select("SELECT rpp.red_packet_money FROM red_packet_pool AS rpp WHERE rpp.create_time BETWEEN #{startTime} AND #{endTime} AND rpp.already_received = 1 AND rpp.receive_nuser_id = #{userId} LIMIT 1")
    Double findRedPacketMoneyByUserId(@Param("startTime") String startTime, @Param("endTime") String endTime,@Param("userId") String userId);

    IPage<RedPacketPageVO> findPageByNowDate(Page<RedPacketPageVO> page, @Param("startTime") String startTime, @Param("endTime") String endTime);

    Date findDateByUpdatetime(@Param("startTime") String startTime, @Param("endTime") String endTime,@Param("sort") String sort);

    @Select("SELECT COUNT(*) FROM red_packet_pool AS rpp WHERE rpp.create_time BETWEEN #{startTime} AND #{endTime}")
    Long findCountByNowDate(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("SELECT IF(SUM(rpp.red_packet_money) IS NULL,0,SUM(rpp.red_packet_money)) FROM red_packet_pool AS rpp WHERE rpp.create_time BETWEEN #{startTime} AND #{endTime}")
    double findSumMoneyByNowDate(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("SELECT rpp.id,nu.nickname,nu.avatar,rpp.red_packet_money AS money FROM red_packet_pool AS rpp LEFT JOIN n_user AS nu ON nu.id = rpp.receive_nuser_id WHERE rpp.create_time BETWEEN #{startTime} AND #{endTime} AND rpp.already_received = 1 ORDER BY money DESC,rpp.update_time DESC LIMIT 35")
    List<UserRedPacketMaxVO> findUserMaxRedPacketByDate(@Param("startTime") String startTime, @Param("endTime") String endTime);


    List<UserRedPacketVO> findUserMinRedPacketByDate(@Param("startTime") String startTime, @Param("endTime") String endTime,@Param("ids") List<Integer> redPacketIds);


}
